package cn.wolfcode.edu.service.impl;

import cn.wolfcode.edu.domain.ClassGrade;
import cn.wolfcode.edu.domain.Classroom;
import cn.wolfcode.edu.mapper.ClassGradeMapper;
import cn.wolfcode.edu.mapper.ClassroomMapper;
import cn.wolfcode.edu.query.QueryObject;
import cn.wolfcode.edu.service.IClassGradeService;
import cn.wolfcode.edu.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by ASUS on 2018/7/8.
 */
@Service
public class ClassGradeServiceImpl implements IClassGradeService {

    @Autowired
    private ClassGradeMapper classGradeMapper;

    @Autowired
    private ClassroomMapper classroomMapper;
    public PageResult query(QueryObject qo) {

        List<ClassGrade> classGradeList=classGradeMapper.queryForList(qo);
        System.out.println(classGradeMapper.queryForList(qo));
        int rows=classGradeMapper.queryForRows(qo);

        return new PageResult(rows,classGradeList);
    }

    public void insertClass(ClassGrade classGrade) {
        //获得当前新增班级的班级名称
        int index = classGrade.getName().lastIndexOf("-");
        String tempName = classGrade.getName().substring(0, index + 1);
        String className = tempName + "大神班";
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(classGrade.getClassEndTime());
        rightNow.add(Calendar.DAY_OF_YEAR,3);//日期加3天
        Date beginTime = rightNow.getTime();
        rightNow.setTime(beginTime);
        rightNow.add(Calendar.DAY_OF_YEAR,140);//日期加3天
        Date endTime = rightNow.getTime();

        //保存基础班
        classGradeMapper.insert(classGrade);
        //设置大神班的班级名字
        classGrade.setName(className);
        //设置大神班开班时间
        classGrade.setClassBeginTime(beginTime);
        //设置大神班结课时间
        classGrade.setClassEndTime(endTime);
        //将班级是否是基础班设置为 非基础班
        classGrade.setBasics(ClassGrade.NO_BASICS);
        //将班级人数设置为0
        classGrade.setStuNumber(0);
        //新增大神班
        classGradeMapper.insert(classGrade);

    }

    public void updateClass(ClassGrade classGrade) {
        classGradeMapper.updateByPrimaryKey(classGrade);
    }

    public List<ClassGrade> listAllClassGradeJson() {
        return classGradeMapper.selectClassGradeNameAndId();
    }

    public Long getIdByName(String name) {
        return classGradeMapper.getIdByName(name);
    }

    @Override
    public void selectClassById(Long id) {
        ClassGrade classGrade = classGradeMapper.selectByPrimaryKey(id);
        Classroom classroom = classGrade.getClassroom();
        Long classroomId = classroom.getId();
        classroomMapper.disableClassroom(classroomId);
    }

    @Override
    public List<ClassGrade> listAllBaseClz() {
        return classGradeMapper.listAllBaseClz();
    }

    @Override
    public List<ClassGrade> listAllGodClz() {
        return classGradeMapper.listAllGodClz();
    }
}
